package com.park.controller;

import com.park.bean.Attracts;
import com.park.bean.Employee;
import com.park.common.CommonResult;
import com.park.dao.IEmployeeDao;
import com.park.service.IEmployeeService;
import com.park.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;

    //展示和查询数据
    @GetMapping("employee")
    public CommonResult getEmployee(Employee employee, Page page){
        List<Employee> res=employeeService.getEmployee(employee,page);
        int count=employeeService.getCount(employee);
        HashMap map=new HashMap();
        map.put("res",res);
        map.put("count",count);
        return CommonResult.success(map);
    }

    //添加数据
    @PostMapping("employee")
    public CommonResult postEmployee(@RequestBody Employee employee){
        int count=employeeService.postEmployee(employee);
        if(count>0){
            return CommonResult.success(employee);
        }else{
            return CommonResult.fail();
        }
    }

    //删除数据
    @DeleteMapping("employee/{id}")
    public CommonResult delEmployee(@PathVariable int id){
        int count= employeeService.delEmployee(id);
        if(count>0){
            return CommonResult.success();
        }else{
            return CommonResult.fail();
        }
    }

    //修改数据
    @PutMapping("employee")
    public CommonResult editEmployee(@RequestBody Employee employee){
        int count=employeeService.editEmployee(employee);
        if(count>0){
            return CommonResult.success(employee);
        }else{
            return CommonResult.fail();
        }
    }
}
